/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import static java.lang.Thread.sleep;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jennerson
 */
public class Pdf {

    public void relatorioTurma(String curso, String turma, String turno, DefaultTableModel tabelaturma) throws IOException {
        Document doc = null;
        OutputStream os = null;
        JFileChooser jf = new JFileChooser();
        String caminho = "";
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf");
        jf.setFileFilter(filter);
        jf.setSelectedFile(new File("Relatório - Turma " + turma + " - " + turno));
        int retorno = jf.showSaveDialog(null);

        if (retorno == JFileChooser.APPROVE_OPTION) {
            caminho = jf.getSelectedFile().getAbsolutePath();

            if (!caminho.equals("")) {
                try {

                    doc = new Document(PageSize.A4.rotate(), 72, 72, 36, 36);
                    os = new FileOutputStream(caminho + ".pdf");
                    PdfWriter.getInstance(doc, os);
                    doc.open();
                    Image img = Image.getInstance("IFPB.png");
                    img.setAlignment(Element.ALIGN_CENTER);
                    doc.add(img);
                    Font f = new Font(FontFamily.COURIER, 24, Font.BOLD);
                    Font f1 = new Font(FontFamily.COURIER, 14, Font.NORMAL);
                    Paragraph p1 = new Paragraph("Relatório por Turma", f);
                    p1.setAlignment(Element.ALIGN_CENTER);
                    doc.add(p1);
                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph("Curso: " + curso, f1));
                    doc.add(new Paragraph("Turno: " + turno, f1));
                    doc.add(new Paragraph("Turma: " + turma, f1));
                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph(" "));

                    PdfPTable table = new PdfPTable(new float[]{0.1f, 0.1f, 0.16f, 0.16f, 0.16f, 0.16f, 0.16f});
                    table.setWidthPercentage(110.0f);
                    table.addCell("Início");
                    table.addCell("Fim");
                    table.addCell("Segunda");
                    table.addCell("Terça");
                    table.addCell("Quarta");
                    table.addCell("Quinta");
                    table.addCell("Sexta");
                    for (int i = 0; i < tabelaturma.getRowCount(); i++) {
                        for (int j = 0; j < 7; j++) {

                            table.addCell(tabelaturma.getValueAt(i, j).toString());

                        }
                    }
                    doc.add(table);

                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "Arquivo sendo executado por outro programa");
                } catch (DocumentException ex) {
                    Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (doc != null) {
                        doc.close();
                    }
                    if (os != null) {
                        os.close();
                    }
                }

            }
        }
    }
    
    public void relatorioProfessor(String professor, String turno, DefaultTableModel tabelaProfessor) throws IOException {
        Document doc = null;
        OutputStream os = null;
        JFileChooser jf = new JFileChooser();
        String caminho = "";
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf");
        jf.setFileFilter(filter);
        jf.setSelectedFile(new File("Relatório - Professor " + professor + " - " + turno));
        int retorno = jf.showSaveDialog(null);

        if (retorno == JFileChooser.APPROVE_OPTION) {
            caminho = jf.getSelectedFile().getAbsolutePath();

            if (!caminho.equals("")) {

                try {

                    doc = new Document(PageSize.A4.rotate(), 72, 72, 36, 36);
                    os = new FileOutputStream(caminho + ".pdf");
                    PdfWriter.getInstance(doc, os);
                    doc.open();
                    Image img = Image.getInstance("IFPB.png");
                    img.setAlignment(Element.ALIGN_CENTER);
                    doc.add(img);
                    Font f = new Font(FontFamily.COURIER, 24, Font.BOLD);
                    Font f1 = new Font(FontFamily.COURIER, 14, Font.NORMAL);
                    Paragraph p1 = new Paragraph("Relatório por Professor", f);
                    p1.setAlignment(Element.ALIGN_CENTER);
                    doc.add(p1);
                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph("Professor: " + professor, f1));
                    doc.add(new Paragraph("Turno: " + turno, f1));

                    doc.add(new Paragraph(" "));
                    doc.add(new Paragraph(" "));

                    PdfPTable table = new PdfPTable(new float[]{0.1f, 0.1f, 0.16f, 0.16f, 0.16f, 0.16f, 0.16f});
                    table.setWidthPercentage(110.0f);
                    table.addCell("Início");
                    table.addCell("Fim");
                    table.addCell("Segunda");
                    table.addCell("Terça");
                    table.addCell("Quarta");
                    table.addCell("Quinta");
                    table.addCell("Sexta");
                    for (int i = 0; i < tabelaProfessor.getRowCount(); i++) {
                        for (int j = 0; j < 7; j++) {

                            table.addCell(tabelaProfessor.getValueAt(i, j).toString());

                        }
                    }
                    doc.add(table);

                    /*PdfPTable table = new PdfPTable(3);
                     PdfPCell header = new PdfPCell(new Paragraph("Tabela Teste!!!"));
                     header.setColspan(7);
                     table.addCell(header);
                     doc.add(table);*/
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "Arquivo sendo executado por outro programa");
                } catch (DocumentException ex) {
                    Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (doc != null) {
                        doc.close();
                    }
                    if (os != null) {
                        os.close();
                    }
                }
            }
        }

    }

    public void relatorioTurno(String curso, List<String> turma, String turno, List<DefaultTableModel> tabelaturno) throws IOException {
        int tamanho = turma.size();
        Document doc = null;
        OutputStream os = null;
        JFileChooser jf = new JFileChooser();
        String caminho = "";
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf");
        jf.setFileFilter(filter);
        jf.setSelectedFile(new File("Relatório - Curso - "+curso+" - Turno - " + turno));
        int retorno = jf.showSaveDialog(null);

        if (retorno == JFileChooser.APPROVE_OPTION) {
            caminho = jf.getSelectedFile().getAbsolutePath();

            if (!caminho.equals("")) {
                
                
                
                try {

                    doc = new Document(PageSize.A4.rotate(), 72, 72, 36, 36);
                    os = new FileOutputStream(caminho + ".pdf");
                    PdfWriter.getInstance(doc, os);
                    doc.open();
                    
                    for (int z = 0; z < tamanho; z++) {
                        Image img = Image.getInstance("IFPB.png");
                        img.setAlignment(Element.ALIGN_CENTER);
                        doc.add(img);
                        Font f = new Font(FontFamily.COURIER, 24, Font.BOLD);
                        Font f1 = new Font(FontFamily.COURIER, 14, Font.NORMAL);
                        Paragraph p1 = new Paragraph("Relatório por Turno", f);
                        p1.setAlignment(Element.ALIGN_CENTER);
                        doc.add(p1);
                        doc.add(new Paragraph(" "));
                        doc.add(new Paragraph(" "));
                        doc.add(new Paragraph("Curso: " + curso, f1));
                        doc.add(new Paragraph("Turno: " + turno, f1));
                        doc.add(new Paragraph("Turma: " + turma.get(z), f1));
                        doc.add(new Paragraph(" "));
                        doc.add(new Paragraph(" "));

                        PdfPTable table = new PdfPTable(new float[]{0.1f, 0.1f, 0.16f, 0.16f, 0.16f, 0.16f, 0.16f});
                        table.setWidthPercentage(110.0f);
                        table.addCell("Início");
                        table.addCell("Fim");
                        table.addCell("Segunda");
                        table.addCell("Terça");
                        table.addCell("Quarta");
                        table.addCell("Quinta");
                        table.addCell("Sexta");
                        for (int i = 0; i < tabelaturno.get(z).getRowCount(); i++) {
                            for (int j = 0; j < 7; j++) {

                                table.addCell(tabelaturno.get(z).getValueAt(i, j).toString());
                                
                            }
                        }
                        doc.add(table);
                        doc.newPage();
                    }
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "Arquivo sendo executado por outro programa");
                } catch (DocumentException ex) {
                    Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (doc != null) {
                        doc.close();
                    }
                    if (os != null) {
                        os.close();
                    }
                }

            }
        }
    }
 
}